package com.chaopei.account_mng.tools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chaopei.account_mng.entity.Account;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
 * @Author Corday 天涯
 * @Date 2022/5/15 9:26
 */
public class MyTools {
    public static void getMaps() {
        String json = "{'1':[{'id':11,'userName':'tom','balance':20},{'id':12,'userName':'bean','balance':21}]," +
                "'2':[{'id':13,'userName':'mary','balance':16},{'id':15,'userName':'coco','balance':17}]}";
        String j = "{'1':{'id':11,'userName':'tom','balance':20},'2':{'id':12,'userName':'bean','balance':21}}";
        //1.转map对象
        Map<Integer,Account> map = JSON.parseObject(j,new TypeReference<Map<Integer,Account>>(){});
        System.out.println("拿到:"+map.get(1).getUserName());
        System.out.println("----------------");
        //2.转map对象与list对象
        Map<Integer, List<Account>> maps = new LinkedHashMap<>();
        maps=JSON.parseObject(json,new TypeReference<Map<Integer,List<Account>>>(){});
        System.out.println(maps.get(1).get(1).getUserName());
    }

    public static void main(String[] args) {
        getMaps();
    }
}
